/**
 * @Description 封装请求
 * @author 焦政
 * @date 2019/6/27
 */
const DeBug = true;
let Connect = {
    baseURL: '', //接口地址
    imgUrl:'http://www.fulianyitong.cn/',//图片地址
};
if (DeBug) {
    // 本地联调
    Connect.baseURL = 'http://www.fulianyitong.cn/api/app/appCall';

} else {
    //正式服
    Connect.baseURL = 'http://192.168.2.116:9001/api/';
}
const net = {
    post(opeType, isdata, options,requestFrom="WeChat") {
        return new Promise((resolve, reject) => {
            wx.request({
                url: Connect.baseURL,
                header: {
                    'content-type': 'application/x-www-form-urlencoded;charset=utf-8'
                },
                method: 'POST',
                data: {
                    "jsonParam": json2Form(options.data, opeType, isdata),
                    "requestFrom": requestFrom
                },
                success: function (res) {
                    if (res == null || res.data == null) {
                        console.log('网络请求失败');
                        return;
                    }
                    resolve(res)
                },
                fail: function (error) {
                    reject(error);
                },
            })
        })
    }
};

function json2Form(json, opeType, isdata) {
    var str = [];
    for (var p in json) {
        str.push('"' + encodeURIComponent(p) + '"' + ":" + '"' + encodeURIComponent(json[p]) + '"');
    }
    var jsondata = '';
    var thereturn = '"opeType":"' + opeType + '"';
    if (isdata == '1') {
        jsondata = ',"map":{' + str.join(",") + "}";
        thereturn = '{' + thereturn + jsondata + '}';
    } else if (isdata == '2') {
        jsondata = ',"map":' + JSON.stringify(json);
        thereturn = '{' + thereturn + jsondata + '}';
    } else {
        thereturn = '{' + thereturn + '}';
    }
    return thereturn;
}

export {net,Connect}